const fs = require("fs");
const fse = require("fs-extra");
const tar = require("tar");
const pkg = require("../package");

const distName = `${pkg.name}.tar.gz`;

module.exports = function pack() {
  try {
    fse.moveSync('./busi', './dist/busi');
    global.console.log('Copying ./busi to ./dist/busi succeeds!');
  } catch (err) {
    global.console.log('Error copying ./busi to ./dist', err);
    process.exit(1);
  }

  try {
    fse.moveSync('./phone', './dist/phone');
    global.console.log('Copying ./phone to ./dist/phone succeeds!');
  } catch (err) {
    global.console.log('Error copying ./phone to ./dist', err);
    process.exit(1);
  }

  tar.c(
    { gzip: true, cwd: `${process.cwd()}/dist` },
    fs.readdirSync("dist/"),
  ).pipe(fs.createWriteStream(distName)).on("finish", () => {
    // eslint-disable-next-line no-console
    console.log(`打包完成，请查看项目目录下的 ${distName}`);
  });
}
